package com.hlingsoft.cms.action.manage;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
 ;
import com.hlingsoft.cms.dao.BrandDao;
import com.hlingsoft.cms.entity.Brand;

import com.hlingsoft.cms.entity.vo.JsonVo;
import com.hlingsoft.cms.entity.vo.PageVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by selbylei on 2015/5/29.
 */

@Controller
@RequestMapping("manage/brand")
public class ManageBrandAction extends ManageBaseAction {

    @Autowired
    private BrandDao brandDao;

    /**
     * 列表页
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String index(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<Brand> pageVo = new PageVo<Brand>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = "/manage/brand/list.htm";
        List<Brand> brandList = brandDao.getBrandList();

        pageVo.setList(brandList);
        pageVo.setCount(brandList.size());
        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);

        return "manage/brand/list";
    }


    /**
     * 添加品牌
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.htm", method = RequestMethod.GET)
    public String add(
            ModelMap modelMap) {
        return "manage/brand/add";
    }

    /**
     * 添加json处理
     * @param file
     * @param name
     * @param request
     * @param modelMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add.json")
    public JsonVo<String> add(
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "file1", required = false) MultipartFile file1,
            @RequestParam(value = "file2",required = false) MultipartFile file2,
            @RequestParam(value = "name") String name,

            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {

            Brand brand = new Brand();
            brand.setName(name);

            //图片上传
            if (file != null) {
                String fileName = file.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file.getInputStream(),
                        file.getSize());
                String logoPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setLogo(logoPath);
            }

            if (file1 != null) {
                String fileName = file1.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file1.getInputStream(),
                        file1.getSize());
                String bannerPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setBanner(bannerPath);
            }

            if (file2 != null) {
                String fileName = file2.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file2.getInputStream(),
                        file2.getSize());
                String goodBannerPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setGoodBanner(goodBannerPath);
            }
            brandDao.addBrand(brand);
            Brand newBrand = brandDao.getBrandById(brand.getId());
            newBrand.setSort(brand.getId());
            brandDao.updateBrand(newBrand);
            json.setResult(true);
            return json;
        } catch (IOException e) {
            e.printStackTrace();
            json.setResult(false);
            return json;
        }
    }


    /**
     * 编辑页面
     * @param brandId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/update.htm", method = RequestMethod.GET)
    public String edit(
            @RequestParam(value = "brandId") long brandId,
            ModelMap modelMap) {
        modelMap.put("brand",brandDao.getBrandById(brandId));
        return "manage/brand/update";
    }


    /**
     * 编辑json处理
     * @param brandId
     * @param file
     * @param name
     * @param request
     * @param modelMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public JsonVo<String> update(
            @RequestParam(value = "brandId") long brandId,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "file1",required = false) MultipartFile file1,
            @RequestParam(value = "file2",required = false) MultipartFile file2,
            @RequestParam(value = "name",required = false) String name,

            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {
            Brand brand = brandDao.getBrandById(brandId);
            if (name!=null){
                brand.setName(name);
            }

            if (file!=null) {
                String fileName = file.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file.getInputStream(),
                        file.getSize());
                String logoPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setLogo(logoPath);
            }
            if (file1!=null) {
                String fileName = file1.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file1.getInputStream(),
                        file1.getSize());
                String bannerPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setBanner(bannerPath);
            }

            if (file2!=null) {
                String fileName = file2.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject1(dateString, fileName, file2.getInputStream(),
                        file2.getSize());
                String goodBannerPath = "http://icake.oss-cn-hangzhou.aliyuncs.com/brand/" + dateString + "/" + fileName;
                brand.setGoodBanner(goodBannerPath);
            }

            brandDao.updateBrand(brand);
            json.setResult(true);
            return json;
        } catch (IOException e) {
            e.printStackTrace();
            json.setResult(false);
            return json;
        }
    }


    /**
     * 删除
     * @param brandId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "brandId") long brandId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        Brand brand = brandDao.getBrandById(brandId);
        brand.setAuthorization(0);
        brandDao.updateBrand(brand);
        json.setResult(true);
        return json;

    }



    public static void putObject1(String date, String filename, InputStream content,
                                 Long size) throws FileNotFoundException {

        // 初始化OSSClient
        OSSClient client = new OSSClient("W6PVM427mSEWlvEz",
                "h7NoXlxcjCqyeOWeuT43LungGmGRSd");

        // 获取指定文件的输入流
        // File file = new File(filePath);
        // InputStream content = new FileInputStream(file);

        // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();

        // 必须设置ContentLength
        meta.setContentLength(size);

        // 上传Object.
        PutObjectResult result = client.putObject("icake", "brand/" + date
                + "/" + filename, content, meta);

        // 打印ETag
        System.out.println(result.getETag());


    }

}
